/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.*;
import Module.*;
import java.util.ArrayList;

/**
 *
 * @author CCD gruppe 2
 */

public class DecisionListController {
    /**
     * Object used to connect to the database
     */
    Database database = new Database();
    /**
     * Method used to register a new decisionlist item
     * @param decisionList
     * @return
     */
    public boolean registerDecisionList(DecisionList decisionList) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());

            String[] autogen = {"id"};
            PreparedStatement statement = conn.prepareStatement(
                    "Insert into decisionList values(DEFAULT,?,?,?,?,?)");
            statement.setInt(1, decisionList.getProjectId());
            statement.setInt(2, decisionList.getSessionId());
            statement.setString(3, decisionList.getDescription());
            statement.setString(4, decisionList.getStatus());
            statement.setString(5, decisionList.getResponsible());
            statement.executeUpdate();
            conn.close();
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
    /**
     * Method used to delete the decisionlist to a project
     * @param projectId
     */
    private void removeDecisionList(int projectId) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "delete from decisionList where projectId=?");
            statement.setInt(1, projectId);
            statement.executeUpdate();
            conn.close();

        } catch (Exception e) {
            System.err.println(e);
        }
    }
    /**
     * Method used to edit a decisionlist to a project
     * @param decisionList
     * @param projectId
     * @return
     */
    public boolean editDecisionList(ArrayList<DecisionList> decisionList, int projectId) {
        Connection conn = null;
        try {
            if (decisionList.size() > 0) {
                conn = DriverManager.getConnection(database.getURL());
                PreparedStatement statement = conn.prepareStatement(
                        "update decisionList set projectId=?, sessionId=?, description=?, status=?,responsible=? "
                        + "where decisionListId=?");
                statement.setInt(1, decisionList.get(0).getProjectId());
                for (int i = 0; i < decisionList.size(); i++) {
                    statement.setInt(2, decisionList.get(i).getSessionId());
                    statement.setString(3, decisionList.get(i).getDescription());
                    statement.setString(4, decisionList.get(i).getStatus());
                    statement.setString(5, decisionList.get(i).getResponsible());
                    statement.setInt(6, decisionList.get(i).getId());
                    statement.executeUpdate();
                }


                conn.close();
                return true;
            }
            else{
                removeDecisionList(projectId);
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
    /**
     * Method used to delete a singel decisionlist item
     * @param decisionList
     * @return
     */
    public boolean deletDecisionList(DecisionList decisionList) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from decisionList where decisionListId=?");
            statement.setInt(1, decisionList.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }

        return false;
    }
    /**
     * Method to get the decisionlist to a project
     * @param projectId
     * @return
     */
    public ArrayList<DecisionList> getDecisionListToProject(int projectId) {
        Connection conn = null;
        ArrayList<DecisionList> liste = new ArrayList<DecisionList>();
        try {
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from decisionList where projectId=?");
            statement.setInt(1, projectId);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                DecisionList dl = new DecisionList(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getString(5), rs.getString(6));
                liste.add(dl);
            }
            rs.close();
            conn.close();
            return liste;

        } catch (Exception e) {
            System.err.println(e);
        }
        return liste;
    }
    /**
     * Method to set the intro to the decisionlist
     * @param projectId
     * @param intro
     */
    public void setIntroduktion(int projectId, String intro) {
        //projectDecionActionIntroduction
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "update projectDecionActionIntroduction set decisionListIntro=? where projectId =? ");
            statement.setString(1, intro);
            statement.setInt(2, projectId);
            statement.executeUpdate();
            conn.close();

        } catch (Exception e) {
            System.err.println(e);
        }
    }
    /**
     * Method used to get the decisionlist intro
     * @param projectId
     * @return
     */
    public String getDecisionListIntro(int projectId) {
        Connection conn = null;
        String value = "";
        try {
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "select decisionListIntro from projectDecionActionIntroduction where projectId=?");
            statement.setInt(1, projectId);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                value = rs.getString(1);
            }
            rs.close();
            conn.close();
            return value;

        } catch (Exception e) {
            System.err.println(e);
        }
        return value;
    }
}
